package com.easy.work.utils;

import java.io.File;
import java.net.URISyntaxException;

import android.app.Activity;
import android.app.AlertDialog;
import android.content.DialogInterface;
import android.content.Intent;
import android.content.SharedPreferences;
import android.net.Uri;
import android.util.Log;

import com.base.syslib.util.MessageTools;
import com.easy.work.R;
import com.easy.work.app.AppSession;

/**
 * OpenMap
 * <p>
 * zhouzhiguo 2015-1-8 上午10:01:58 调用地图apk
 *
 * @version 1.0.0
 */
public class OpenMap {

    //	private static Activity act;
    private String gaoDePackage = "com.autonavi.minimap";
    private String baiDuPackage = "com.baidu.BaiduMap";
    private String purposeLatitude;// 目的纬度
    private String purposeLongitude;// 目的精度


    /**
     * 地图选择框
     **/
    public void openMap(Activity activity, String companyName, String Latitude, String Longitude) {

//		act = activity;
        purposeLatitude = Latitude;
        purposeLongitude = Longitude;
        try {
            if (!isInstallByread(gaoDePackage) && !isInstallByread(baiDuPackage)) {// 高德，百度地图都没有安装
                MessageTools.showDialogOk(activity, "您的手机未安装高德或者百度地图！");
            } else if (isInstallByread(gaoDePackage) && isInstallByread(baiDuPackage)) {// 高德，百度地图都安装了
                showMapType(activity, companyName);
            } else if (isInstallByread(baiDuPackage)) {
                openbaiduMap(activity, companyName);
            } else if (isInstallByread(gaoDePackage)) {
                openGaoDeMap(activity, companyName);
            }
        } catch (Exception e) {
            e.printStackTrace();
        }
    }

    public void openBaiDuMap(Activity activity, String companyName, String Latitude, String Longitude) {

//		act = activity;
        purposeLatitude = Latitude;
        purposeLongitude = Longitude;
        try {
            if (!isInstallByread(baiDuPackage)) {// 高德，百度地图都没有安装
                MessageTools.showDialogOk(activity, "您的手机未安装百度地图，请先安装百度地图。");
//            } else if (isInstallByread(gaoDePackage) && isInstallByread(baiDuPackage)) {// 高德，百度地图都安装了
//                showMapType(activity, companyName);
            } else if (isInstallByread(baiDuPackage)) {
                openbaiduMap(activity, companyName);
//            } else if (isInstallByread(gaoDePackage)) {
//                openGaoDeMap(activity, companyName);
            }
        } catch (Exception e) {
            e.printStackTrace();
        }
    }

    private int nodeQueryWaySelectedId; // 记录列表选项的值(我的附近)


    /**
     * 启动地图导航方式 高德 百度
     */
    protected void showMapType(final Activity act, final String companyName) {

        AlertDialog.Builder builder = new AlertDialog.Builder(act);
        builder.setTitle("请选择地图导航方式");
        builder.setIcon(android.R.drawable.ic_dialog_info);
        final String[] array = act.getResources().getStringArray(R.array.map_type);
        builder.setSingleChoiceItems(array, nodeQueryWaySelectedId, new DialogInterface.OnClickListener() {

            public void onClick(DialogInterface dialog, int which) {

                nodeQueryWaySelectedId = which;
            }
        });
        builder.setNegativeButton("取消", new DialogInterface.OnClickListener() {

            public void onClick(DialogInterface dialog, int which) {

                dialog.dismiss();
            }
        });
        builder.setPositiveButton("确定", new DialogInterface.OnClickListener() {

            public void onClick(DialogInterface dialog, int which) {

                if (nodeQueryWaySelectedId >= 0) {
                    String type = act.getResources().getStringArray(R.array.map_type)[nodeQueryWaySelectedId];
                    // 0:高德地图
                    if (0 == nodeQueryWaySelectedId) {
                        openGaoDeMap(act, companyName);

                    } else if (1 == nodeQueryWaySelectedId) {// 百度地图
                        openbaiduMap(act, companyName);
                    }
                }
                dialog.dismiss();
            }
        });

        builder.create().show();
    }


    /**
     * openGaoDeMap void 调用高德地图apk
     *
     * @throws
     * @since 1.0.0
     */
    private void openGaoDeMap(Activity act, String companyName) {

        Intent intent = new Intent("android.intent.action.VIEW",
                android.net.Uri.parse("androidamap://navi?sourceApplication=" + companyName + "&lat=" + purposeLatitude + "&lon="
                        + purposeLongitude + "&dev=0"));
        intent.setPackage("com.autonavi.minimap");
        act.startActivity(intent);
    }


    /**
     * openbaiduMap void 调用百度地图apk
     *
     * @throws
     * @since 1.0.0
     */
    private void openbaiduMap(Activity act, String companyName) {
//        String posX = settings.getString("LAST_LOCAL_LATITUDE", "0"); // 纬度
//        String posY = settings.getString("LAST_LOCAL_LONGITUDE", "0"); // 经度

        String posX = AppSession.getInstance().getLatitude(); // 纬度
        String posY = AppSession.getInstance().getLontitude(); // 经度
        try {
            @SuppressWarnings("deprecation")
            Intent intent = Intent.getIntent("intent://map/direction?origin=latlng:" + posX + "," + posY
                    + "|name:我的位置&destination=latlng:" + purposeLatitude + "," + purposeLongitude
                    + "|&mode=driving&src=" + companyName + "|" + companyName + "#Intent;scheme=bdapp;package=com.baidu.BaiduMap;end");
            act.startActivity(intent); // 启动调用
//
//            Intent i1 = new Intent();
//
//// 驾车路线规划
//
//            i1.setData(Uri.parse("baidumap://map/direction?region=beijing&origin=" + purposeLatitude + "," + purposeLongitude + "&destination=" + companyName + "&mode=driving"));
//
//            act.startActivity(i1);
        } catch (URISyntaxException e) {
            e.printStackTrace();
        } catch (Exception e) {
            e.printStackTrace();
        }
    }


    // 判断是否安装了应用
    private boolean isInstallByread(String packageName) {

        return new File("/data/data/" + packageName).exists();
    }
}
